package models;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;
import java.util.logging.Logger;

import play.Play;
import play.vfs.VirtualFile;

public class SiteConfig {
	private Properties properties;

	private SiteConfig() {
		this.properties = new Properties();
	}

	private static SiteConfig INSTANCE;

	public static SiteConfig load() {
		if (null == INSTANCE) {
			SiteConfig siteConfig = new SiteConfig();

			try {
				siteConfig.loadConfig();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}

			INSTANCE = siteConfig;
		}

		return INSTANCE;
	}

	private void loadConfig() throws IOException {
		VirtualFile virtualFile = Play.modules.get("siteconfig");

		virtualFile = virtualFile.child("conf/application.conf");

		properties.load(virtualFile.inputstream());

		{
			StringWriter writer = new StringWriter();

			properties.store(writer, "Properties");

			Logger.getLogger(SiteConfig.class.getName())
					.info(writer.toString());
		}
	}

	public Properties getProperties() {
		return properties;
	}

	public String getConfig(String key) {
		return properties.getProperty(key);
	}
}
